<?php

class SQL extends MySQLi {
	
	function __construct($host, $login, $pass, $db) {
		parent::__construct($host, $login, $pass, $db);
		
		if($this->connect_error) {
			klog_error('Erreur de connexion : ' . $this->connect_error);
		}
	}
	
	function close() {
		parent::close();
	}
	
	function query($query, $resultmode = null) {
		$result = parent::query($query, $resultmode);
		
		if(!$result) {
			 klog_error($query.' => '.$this->error);
		}
		
		return $result;
	}
	
	/** 
		METHOD : insertInto
		
		$table => la table concernée
		$hashmap => le tableau associatif contenant les attributs/valeurs à insérer
	
		Exemple d'utilisation
		$hashmap['nom'] = "Mousquetaire";
		$hashmap['prenom'] = "Albert";
		$hashmap['mdp'] = md5("albert");
		$hashmap['email'] = "albert.mousquetaire@projbooster.com";
		$hashmap['tel_fix'] = "0251879865";
		$hashmap['tel_port'] = "0684986575";
		$hashmap['est_admin'] = 0;
		$db->insertInto("utilisateur",$hashmap);
	**/
	function insertInto($table,$hashmap){
		$query = "INSERT INTO $table(";
		
		$endquery = ") VALUES (";
		foreach ($hashmap as $key => $value) {
			$query .= $key.",";
			if((!is_numeric($value)) || ($value[0] == 0)){
				$endquery .= "'".$value."',";
			}else{
				$endquery .= $value.",";
			};
		};
		$endquery = substr($endquery, 0, strlen($endquery)-1);
		$query = substr($query, 0, strlen($query)-1);
		
		$query .= $endquery.");";
		$this->query($query);
	}
	
	function secureData($data) {
		return parent::real_escape_string(htmlspecialchars($data));
	}
	
		/**
		METHOD : updateOn
		
		$table => la table concernée
		$hashmap => le tableau associatif contenant les attributs/valeurs à modifier
		$condition => les conditions WHERE à écrire sous forme SQL.
		
		$hashmap['nom'] = $_POST['nom'];
		$hashmap['prenom'] = $_POST['prenom'];
		$hashmap['tel_fix'] = $_POST['tel_fix'];
		$hashmap['tel_port'] = $_POST['tel_port'];
		$db->updateOn("utilisateur",$hashmap,"utilisateur_id = $id");
	**/
	function updateOn($table,$hashmap,$condition = ""){
		$query = "UPDATE $table SET ";
		
		foreach ($hashmap as $key => $value) {
			$query .= $key." = ";
			if((!is_numeric($value)) || ($value[0] == 0)){
				$query .= "'".$this->secureData($value)."',";
			}else{
				$query .= $this->secureData($value).",";
			};
		};
		$query = substr($query, 0, strlen($query)-1);
		
		if($condition == ""){
			$query .= " ;";
		}else{
			$query .= " WHERE ".$condition." ;";
		};
		$this->query($query);	
	}
	
		/**
		METHOD : select
		
		$champs => les champs à récupérer
		$tables => les tables concernées
		$naturalJoin => applique des natural join ou non
		$condition => les conditions WHERE à écrire sous forme SQL.
		
		$champs[] = "login";
		$champs[] = "users.login";
		$champs[] = "count(*) as toto";
		$champs[] = "password";
		
		// il est aussi possible d'écrire simplement
		$champs[] = "login,password";
		
		$tables[] = "users";
		$tables[] = "bidule";
		
		$db->select($champs,$tables,true,""); // natural join
		$db->select($champs,$tables,false,"users.user_id == bidule.user_id"); // pas de natural join
	**/
	function select($champs,$tables,$naturalJoin,$condition = "",$groupBy = "",$having = "",$orderBy = "",$limit = ""){
		$query = "SELECT ";
		if(count($champs) > 1){
			foreach ($champs as $name) {
				$query .= $name.",";
			};
			$query = substr($query, 0, strlen($query)-1);
		}else{
				$query .= $champs;
		};
		$query .= " FROM ";
		if(count($tables) > 1){
			for($i = 0;$i < count($tables);$i++){
				if((count($tables)-1) == $i){
					$query .= $tables[$i];
				}else if(!$naturalJoin){
					$query .= $tables[$i].",";
				}else{
					$query .= $tables[$i]." NATURAL JOIN ";
				};
			};
		}else{
				$query .= $tables;
		};
		if($condition != ""){
			$query .= " WHERE ".$condition;
		};
		
		if($groupBy != ""){
			$query .= " GROUP BY ".$groupBy;
		};

		if($having != ""){
			$query .= " HAVING ".$having;
		};

		if($orderBy != ""){
			$query .= " ORDER BY ".$orderBy;
		};
		
		if($limit == ""){
			$query .= " ;";
		}else{
			$query .= " LIMIT ".$limit." ;";
		};
		
			return $this->query($query);
		}
}

?>
